﻿using System;
using System.Collections.Generic;
using System.Linq;
using Medianamik.Core;
using Medianamik.Core.Security;

namespace Medianamik.UI.Web.ContentManagement
{
    public partial class GroupSelector : BasePage
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (!Page.IsPostBack)
            {
                var groups = SecurityManager.AllGroups.ToList();
                groups.RemoveAll(g => g.ID.Equals(MedianamikGroups.SuperAdmins));

                var nodeIds = GetNodeIdsToExclude();
                if(nodeIds.Count > 0)
                {
                    groups.RemoveAll(x => nodeIds.Contains(x.ID.ToString()));
                }

                GroupList.DataSource = groups.OrderBy(x => x.TranslatedName);
                GroupList.DataBind();
                GroupList.Items.Insert(0, "");
            }
        }

        private List<string> GetNodeIdsToExclude()
        {
            var nodeIds = new List<string>();
            var without = Request.QueryString["without"];
            if(!string.IsNullOrEmpty(without))
            {
                nodeIds = without.Split(',').ToList();
            }
            return nodeIds;
        }
    }
}
